package com.example.wechat.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wechat.R;
import com.example.wechat.ui.Mode;

import java.util.List;

public class Recycler_Adapter extends RecyclerView.Adapter<Recycler_Adapter.ViewHolder> {

    List<Mode> mlist;
    public Recycler_Adapter(List<Mode> list)
    {
        this.mlist=list;
}

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.mode_view,parent,false);
       ViewHolder holder=new ViewHolder(view);
       view.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               int i=holder.getAdapterPosition();
               Mode mode=mlist.get(i);
               Toast.makeText(view.getContext(), "你点击了"+mode.getTitle()+"\n"+mode.getTime(), Toast.LENGTH_SHORT).show();
           }
       });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Mode mode=mlist.get(position);
        holder.img.setImageResource(mode.getImg());
        holder.title.setText(mode.getTitle());
        holder.nr.setText(mode.getNr());
        holder.time.setText(mode.getTime());
    }


    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView title;
        TextView nr;
        TextView time;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.t_img);
            title=itemView.findViewById(R.id.t_title);
            nr=itemView.findViewById(R.id.t_content);
            time=itemView.findViewById(R.id.t_time);
        }
    }
}


